<?php

include_once( dirname(__FILE__).'/../basic/cfield.php' );

class CFFile extends CField
{
#-------------------------------------------------------------------------------------------------------------------------
	var $upload_dir = '';

#-------------------------------------------------------------------------------------------------------------------------

	function __set_params( &$params ){
		parent :: __set_params( $params );
		$this->upload_dir = $params['uploaddir'];
		if ( !is_writeable($this->upload_dir) )
		    $this->__raise_error(__FDC_ERR_UPLOAD_DIR, __FILE__, __LINE__, $this->upload_dir);
	}

#-------------------------------------------------------------------------------------------------------------------------

	function store_uploaded_file(){
		$res = '';
		$latin_equivalent = array(
									32 => '_',
									95 => '_',
									168 => 'YO',
									184 => 'yo',
									192 => 'A',
									193 => 'B',
									194 => 'V',
									195 => 'G',
									196 => 'D',
									197 => 'E',
									198 => 'ZH',
									199 => 'Z',
									200 => 'I',
									201 => 'Y',
									202 => 'K',
									203 => 'L',
									204 => 'M',
									205 => 'N',
									206 => 'O',
									207 => 'P',
									208 => 'R',
									209 => 'S',
									210 => 'T',
									211 => 'U',
									212 => 'F',
									213 => 'KH',
									214 => 'TS',
									215 => 'CH',
									216 => 'SH',
									217 => 'SHCH',
									218 => '',
									219 => 'Y',
									220 => '',
									221 => 'E',
									222 => 'U',
									223 => 'YA',
									224 => 'a',
									225 => 'b',
									226 => 'v',
									227 => 'g',
									228 => 'd',
									229 => 'e',
									230 => 'zh',
									231 => 'z',
									232 => 'i',
									233 => 'y',
									234 => 'k',
									235 => 'l',
									236 => 'm',
									237 => 'n',
									238 => 'o',
									239 => 'p',
									240 => 'r',
									241 => 's',
									242 => 't',
									243 => 'u',
									244 => 'f',
									245 => 'kh',
									246 => 'ts',
									247 => 'ch',
									248 => 'sh',
									249 => 'shch',
									250 => '',
									251 => 'y',
									252 => '',
									253 => 'e',
									254 => 'u',
									255 => 'ya'
								);
		$org_name = $_FILES[$this->name]['name'];
		$tmp_name = $_FILES[$this->name]['tmp_name'];
		$new_name = '';
		#получаем транслит имя
		$index = strlen($org_name);
		for($i=0; $i<$index; $i++){
			$ord = ord($org_name[$i]);
			if ( array_key_exists( $ord, $latin_equivalent ) ){
				$new_name .= $latin_equivalent[$ord];
			}else{				$new_name .= $org_name[$i];
			}
		}
		#смотрим есть ли уже файл с таким именем
		if ( file_exists($this->upload_dir.'/'.$new_name) ){
			$fd 	= $this->__get_ext($new_name);
			$ind 	= 0;
			#перебираем имена пока не найдем несуществующего
			while( true ){
				$new_ind_name = $fd['name'].'_'.$ind.($fd['ext'] ? '.'.$fd['ext'] : '');
				if( !file_exists($this->upload_dir.'/'.$new_ind_name) ){
					$new_name = $new_ind_name;
					break;
				}else{					$ind++;
				}
			}
		}
		@move_uploaded_file($tmp_name, $this->upload_dir.'/'.$new_name);
	}

#-------------------------------------------------------------------------------------------------------------------------

	function __get_ext($fname){
		$res = array();
		$buf = explode('.', $fname);
		$index = sizeof($buf);
		if ( $index == 1 ){
			$res['name'] = $fname;
		}else{
			$res['ext'] = $buf[$index - 1];
			unset($buf[$index - 1]);
			$res['name'] = implode('.', $buf);
		}
		return $res;
	}

#-------------------------------------------------------------------------------------------------------------------------

}

?>